<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/22
 * Time: 1:11
 */

namespace app\admin\controller;


use think\Controller;

/**
 * Class Brand
 * @package app\admin\controller
 * 品牌控制器
 */
class Brand extends Controller
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Brand();
    }
    //品牌主页
    public function index(){
        //分配数据
        $data=$this->db->select();
        $this->assign('data',$data);
        //视图
        return view();
    }
    //品牌添加何编辑
    public function add(){
        $bid=input('param.bid');
        if($bid !== null) {
            //编辑
            $oldData = $this->db->where('bid',$bid)->find();
            if(request()->isPost()){
                $data = input('post.');
                $res = $this->db->add($data);
                if($res['valid']){
                    $this->success($res['msg'],'index');
                }else{
                    $this->error($res['msg']);
                }
            }
        }else {
            //添加
            $oldData['bid']='';
            $oldData['bname']='';
            $oldData['hot']='';
            $oldData['logo']='';
            $oldData['sort']='';
            if (request()->isPost()) {
                $data = input('post.');
                $res = $this->db->add($data);
                if ($res['valid']) {
                    $this->success($res['msg'], 'index');
                } else {
                    $this->error($res['msg']);
                }
            }
        }
        $this->assign('oldData', $oldData);
        return view();
    }
    //品牌删除
    public function del($bid){
        $tid=input('param.tid');
        $res=$this->db->del($bid);
        if ($res['valid']) {
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }
}